package com.it.exprezoorecords.json.activity;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import utility.ApplicationProperties;
import utility.Networking;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.it.exprezoorecords.R;
import com.it.exprezoorecords.R.string;
import com.it.exprezoorecords.json.JSONfunctions;


public class EventsActivity extends ListActivity{
	JSONArray eventsList = null;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        
        if(Networking.isNetworkAvailable(EventsActivity.this)){
        	new BackgroundWorker().execute();
        	setContentView(R.layout.listplaceholderevents);  
        }else{
        	setContentView(R.layout.listplaceholderevents);  
        	Toast.makeText(EventsActivity.this, string.noconnection, Toast.LENGTH_LONG).show();   	
        }        	          
        final ListView lv = getListView();
        lv.setTextFilterEnabled(true);	
        lv.setOnItemClickListener(new OnItemClickListener() {
        	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        		Intent intentWv = new Intent(getApplicationContext(), EventsWebViewActivity.class);  
        		JSONObject currentObj;
				try {
					currentObj = eventsList.getJSONObject(position);
					intentWv.putExtra("weblink", currentObj.getString("link"));
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
        		
        		startActivity(intentWv);
        	}
		});

    }
    
    
    class BackgroundWorker extends AsyncTask<Void, Void, Void>{
    	ArrayList<HashMap<String, String>> mylist;
    	private ProgressDialog myProgressDialog;
	    @Override
	    protected void onPreExecute() {
	    	// TODO Auto-generated method stub
	    	super.onPreExecute();
	    	myProgressDialog=ProgressDialog.show(EventsActivity.this, "", "Your Exprezoo Events...", true);
	    }
	    
    	@Override
		protected Void doInBackground(Void... arg0) {
			// TODO Auto-generated method stub
            mylist = new ArrayList<HashMap<String, String>>();
            JSONObject json = JSONfunctions.getJSONfromURL(ApplicationProperties.EVENTI_JSON_URL);
                    
            try{        	
            	eventsList = json.getJSONArray("event");
            	for(int i=0;i<eventsList.length();i++){						
    				HashMap<String, String> map = new HashMap<String, String>();	
    				JSONObject e = eventsList.getJSONObject(i);
    				map.put("id",  String.valueOf(i));
    	        	map.put("description",  e.getString("description"));
    	        	map.put("link", "link: " +  e.getString("link"));
    	        	mylist.add(map);			
    			}		
            }catch(JSONException e) {
            	 Log.e("log_tag", "Error parsing data "+e.toString());
            }

			return null;
		}
    	
		@Override
		protected void onPostExecute(Void result)
		{
	        ListAdapter adapter = new SimpleAdapter(EventsActivity.this, mylist , R.layout.jsonevent, 
	                        new String[] { "description"}, 
	                        new int[] { R.id.item_title });
	        
	        setListAdapter(adapter);
	        myProgressDialog.dismiss();
			
		}
    }
}
